﻿///<reference path="../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../scripts/jquery-ui.min.js" />
///<reference path="../../scripts/kooboo/kooboo.form.js" />

$(function () {
    function render() {
        var isEven = false;
        $("#review-container").empty();

        for (var i = 0; i < Reviews.length; i++) {
            var item = Reviews[i];

            var row = $("#review-row").find(".template").clone();
            var product = row.find("td:eq(0)").find("a");
            product.attr("href", item.ProductUrl);
            product.html(item.ProductName);

            row.find("td:eq(1)").html(item.Rate);

            var customer = row.find("td:eq(2)").find("a");
            customer.attr("href", item.CustomerUrl);
            customer.html(item.CustomerName);

            row.find("td:eq(3)").html(item.CreateDate);

            if (item.IsPublished === true) {
                row.find("td:eq(4)").find(".cross").hide();
            } else {
                row.find("td:eq(4)").find(".tick").hide();
                row.addClass("todo");
            }

            row.find("td:eq(5)").find("a").attr("data-index", i);
            row.find("td:eq(6)").find("form").attr("data-index", i);
            row.find("td:eq(6)").find("input[name=Id]").val(item.Id);

            if (isEven) {
                row.addClass("even");
            }

            isEven = !isEven;

            $("#review-container").append(row);
        }
    }

    render();


    //events
    //1.view
    $(".view", "#review-container").live("click", function (e) {
        e.preventDefault();

        var index = parseInt($(this).attr("data-index"));

        var item = Reviews[index];

        var dialog = $("#view-dialog");

        var product = dialog.find(".product");
        product.html(item.ProductName);
        product.attr("href", item.ProductUrl);

        var customer = dialog.find(".customer");
        customer.html(item.CustomerName);
        customer.attr("href", item.CustomerUrl);

        dialog.find(".rate").html(item.Rate);
        dialog.find(".remark").html(item.Remark);

        dialog.find(".id").val(item.Id);
        dialog.find(".publish-form").attr("data-index", index);
        dialog.find(".buttons span").show();
        if (item.IsPublished) {
            dialog.find(".buttons .publish").hide();
        } else {
            dialog.find(".buttons .unpublish").hide();
        }
        return false;
    });


    //2.publish
    $(".publish-form", "#view-dialog").live("submit", function () {

        var form = $(this);

        form.ajaxPost({
            success: function () {
                var index = parseInt(form.attr("data-index"));
                var item = Reviews[index];
                item.IsPublished = !item.IsPublished;

                render();

                $("#view-dialog").dialog("close");
            }
        });

        return false;
    });

    //3.delete
    $(".delete-link", "#review-container").live("click", function (e) {
        e.preventDefault();
        if (confirm($(this).attr("message"))) {
            $(this).parents("form:first").submit();
        }

        return false;
    });

    $(".delete-form", "#review-container").live("submit", function () {
        var form = $(this);

        form.ajaxPost({
            success: function () {
                var index = parseInt(form.attr("data-index"));
                Reviews.splice(index, 1);
                render();
            }
        });

        return false;
    });

});